package com.wei;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class LockHelper {
	
	private static Map<String,ReentrantLock> lockMap = new ConcurrentHashMap();
	
	private static Object M_LOCK = "$$__LOCKHELPER_M_LOCK";
	
	public static void lock(String key){
		ReentrantLock lock = lockMap.get(key);
		
		if( lock == null )
		{
			synchronized ( M_LOCK)
			{
				lock = lockMap.get(key);
				if( lock == null )
				{
					lock = new ReentrantLock();
					lockMap.put(key, lock);
				}				
			}		
		}
		lock.lock();
	}
	
	public static void unlock(String key)
	{
		ReentrantLock lock = lockMap.get(key);
		lock.unlock();
	}
	
	
	public static void clear(String key)
	{
		lockMap.remove(key);
	}
}
